jQuery(document).ready(function($) {

	var sorce = decodeURIComponent((new RegExp('[?|&]sorce=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	var lucky_record_id = decodeURIComponent((new RegExp('[?|&]lucky_record_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	var datac = decodeURIComponent((new RegExp('[?|&]datac=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	var productid = decodeURIComponent((new RegExp('[?|&]product_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	if (sorce == 4) {
		$('.duihuan').html('立即领取')
	}
	// 更换地址
	$('.order_address').click(function(event) {
		if (sorce == 4) {
			// alert(product_id)
			window.location.href = addressurl + '?product_id=' + productid + '&sorce=' + sorce+ '&lucky_record_id=' + lucky_record_id+ '&datac=' + datac
		}else{
			window.location.href = addressurl + '?product_id=' + productid + '?sorce=1' 
		}
		
		// $.ajax({
		// 	url: addressurl,
		// 	type: 'POST',
		// 	data: {
		// 		address_id: $('.address').attr('data-address'),
		// 		product_id:product_id,
		// 		attr_id:attr_id,
		// 		sorce:1,
		// 		num:num
		// 	},
		// 	success:function(){
		// 		window.location.href= addressurl+'?product_id='+product_id
		// 	}
		// })
	});


	// 兑换
	$('.duihuan').click(function(event) {

		if (sorce == 4) {
			$.ajax({
				url: url,
				type: 'POST',
				data: {
					address_id: $('.address').attr('data-address'),
					product_id:productid,
					attr_id:datac,
					lucky_record_id:lucky_record_id,
					sorce:sorce
				},
				success:function(data){
					console.log(data)
					var data = JSON.parse(data)
					if (data.code != 1) {
						alert(data.desc)
					}else{
						window.location.href=order_detail_url+"?order_id="+data.data+"&sorce="+sorce
					}
				},
				error:function(xhr, textStatus){
					if(xhr.status == 401){
						 window.location.href = "/Npoint_store/Login";
					}else{
						alert('系统错误')
					}

				}
			})

		}else{
		
			if(!getCookie('product_order')){
				alert('请勿重复提交订单')
			}
			
			var url_pro = JSON.parse(getCookie('product_order'));
			var product_id = url_pro.product_id
			var attr_id = url_pro.attr_id
			var num = url_pro.num
			$.ajax({
				url: url,
				type: 'POST',
				data: {
					address_id: $('.address').attr('data-address'),
					product_id:product_id,
					attr_id:attr_id,
					sorce:1,
					num:num
				},
				success:function(data){
					var data = JSON.parse(data)
					if (data.code != 1) {
						alert(data.desc)
					}else{
						window.location.href=order_detail_url+"?order_id="+data.data
						SetCookie('product_order', '')
					}
				},
				error:function(xhr, textStatus){
					if(xhr.status == 401){
						 window.location.href = "/Npoint_store/Login";
					}else{
						alert('系统错误')
					}

				}
			})
		}
	});



});